<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<button id="insert" class="btn btn-success" onclick="divIdUse('insert',null,${pageInfo.pageNo})">添加</button>
	<table class="table table-striped table-bordered table-hover table-condensed">
		<tr>
			<th>ID</th>
			<th>姓名</th>
			<th>年龄</th>
			<th>密码</th>
			<th>邮箱</th>
			<th>部门ID</th>
			<th>删除</th>
			<th>修改</th>
		</tr>
		<c:forEach items="${pageInfo.list}" var="user">
			<tr>
			<td>${user.id}</td>
			<td>${user.name}</td>
			<td>${user.age}</td>
			<td>${user.password}</td>
			<td>${user.email}</td>
			<td>${user.department_id}</td>
			<td><a href="javascript:deleteById(${user.id},${pageInfo.pageNo})">删除</a></td>
			<td><a href="javascript:divIdUse('update',${user.id},${pageInfo.pageNo})">修改</a></td>
		</tr>
		</c:forEach>
	</table>
	<nav aria-label="Page navigation">
		  <ul class="pagination">
		  		<c:choose>
		  			<c:when test="${pageInfo.pageNo == 1}">
		  				<li>
		      				<a aria-label="Previous">
		        				<span aria-hidden="true">&laquo;</span>
		     		 		</a>
		    			</li>
		  			</c:when>
		  			<c:otherwise>
		  				<li>
		      				<a href="${path}/user?method=selectByPage&pageNo=${pageInfo.pageNo - 1}" aria-label="Previous">
		        				<span aria-hidden="true">&laquo;</span>
		     		 		</a>
		    			</li>
		  			</c:otherwise>
		  		</c:choose>
		  		
		  		
		  		<c:forEach begin="1" end="${pageInfo.totalPage}" var="i">
		  			<c:choose>
		  				<c:when test="${pageInfo.pageNo == i}">
		  					<li>
		    					<a aria-label="Previous">
		        					<span aria-hidden="true">${i}</span>
		     		 			</a>
		    				</li>
		  				</c:when>
		  				<c:otherwise>
		  					<li>
		    					<a href="${path}/user?method=selectByPage&pageNo=${i}" aria-label="Previous">
		        					<span aria-hidden="true">${i}</span>
		     		 			</a>
		    				</li>
		  				</c:otherwise>
		  			</c:choose>
		  			
		  		</c:forEach>
		    	
		    	
		    	<c:choose>
		    		<c:when test="${pageInfo.pageNo == pageInfo.totalPage}">
		  				<li>
		      				<a aria-label="Previous">
		        				<span aria-hidden="true">&raquo;</span>
		     		 		</a>
		    			</li>
		  			</c:when>
		  			<c:otherwise>
		  				<li>
		      				<a href="${path}/user?method=selectByPage&pageNo=${pageInfo.pageNo + 1}" aria-label="Previous">
		        				<span aria-hidden="true">&raquo;</span>
		     		 		</a>
		    			</li>
		  			</c:otherwise>
		  			</c:choose>
		  </ul>
	</nav>
	<script type="text/javascript">
		layer.ready(function(){
			$.post("${path}/user?method=selectDepartment",
					function(date){
						$("#divId input").val("");
						$(date).each(function(){
							console.log(this);
							var option = "<option value='"+this.id+"'>"+this.name+"</option>";
							$("#selectId").append(option);
						});
					},"json");
		});
		function deleteById(id,pageNo) {
			
			/* var isDelete = confirm("您确定要删除么？");
			if(isDelete){
				location.href = "${path}/user?method=deleteById&id="+id;
			} */
			
			mylayer.confirm("您确定要删除么？" , "${path}/user?method=deleteById&id=" + id + "&pageNo=" + pageNo);
		}
		
		function divIdUse(method , id , pageNo) {
			layer.open({
				type:1,
				content:$("#divId"),
				area : ["500px","300px"]
			});
			switch(method){
			case 'insert' :
				$("#change").html("添加");
				$("#divId input,select").val("");
				$("#form").attr("action","${path}/user?method=insert&pageNo=" + pageNo);
				break;
				
			case 'update' :
				$("#change").html("修改");
				$.post("${path}/user?method=selectById&id="+id,
						function(date){
							$(date).each(function(){
								$("#divId input:eq(0)").val(this.name);
								$("#divId input:eq(1)").val(this.password);
								$("#divId input:eq(2)").val(this.email);
								$("#divId input:eq(3)").val(this.age);
								$("#divId select").val([this.department_id]);
							});
						},'json');
				$("#form").attr("action","${path}/user?method=updateById&id=" + id + "&pageNo=" + pageNo)
				break;
			}
		}
	</script>
</body>
<div id="divId" style="display:none;">
	<form id="form" method="post">
		<table class="table table-striped table-bordered table-hover table-condensed">
			<tr>
				<td>
					用户：<input type="text" name="name"/>
				</td>
			</tr>
			<tr>
				<td>
					密码：<input type="text" name="password"/>
				</td>
			</tr>
			<tr>
				<td>
					邮箱：<input type="text" name="email"/>
				</td>
			</tr>
			<tr>
				<td>
					年龄：<input type="text" name="age"/>
				</td>
			</tr>
			<tr>
				<td>
					部门：
					<select id="selectId" name="department_id">
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<button type="submit" id="change">添加</button>
				</td>
			</tr>
		</table>
	</form>
</div>
</html>